// pages/login/login.js
const api=require('../../utils/request') 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked1: false,
    type: '',
    reportId:'',
    isAfterShow: false,
    image:'',
    backType:''
  },
 
  checkedTap(){ 
      let checked=!this.data.checked1
      this.setData({
          checked1: checked
      })
  },
  checkgetPhoneNumber(){
    wx.showToast({
        title: '请阅读并同意相关协议',
        icon: 'none',
        duration: 2000,
      })
   


  },
  loginAfterClose(){
      this.setData({
        isAfterShow: false
      })
      this.backPage()
  },
  login(){
      let that=this
    wx.showModal({
        title: '新研报用户使用协议',
        content: '进入下一步，请阅读并同意《新研报用户使用协议》',
        confirmText:'同意',
        success (res) {
          if (res.confirm) {
             wx.login({
               success: (res) => {
                console.log(res)
                api.post('wechat/login',{code:res.code}).then(response=>{
                    let user=response.data;

                    wx.setStorageSync('user',JSON.stringify(user));  
                    if(user.isShow){
                        that.setData({
                            isAfterShow: true,
                            image: user.image
                           })
                    }else{
                         that.backPage()
                        }
                                    })
               },
             })
          } else if (res.cancel) {
             
          }
        }
      })
      
  },
  phone(){
    wx.navigateTo({
        url: '../phone/phone?type='+this.data.type+'&reportId='+this.data.reportId+'&backType='+this.data.backType
      })
  },
backPage(){
    if(this.data.type==='1'){
        wx.switchTab({
            url: '/pages/report/report',
            success: function (e) {
              let page = getCurrentPages().pop(); 
              page.onLoad();
            }
          })
    }else if(this.data.type==='2'){
        wx.redirectTo({
            url: '../vip/buy_vip/buy_vip'
          })
          
         
    }else if(this.data.type==='3'){
         
        wx.redirectTo({
            url: '/pages/report/pdf/pdf?id='+this.data.reportId+'&backType='+this.data.backType
          })
    }else{
        wx.switchTab({
            url: '/pages/mine/mine',
            success: function (e) {
              let page = getCurrentPages().pop(); 
              page.onLoad();
            }
          })
    }
},
  getPhoneNumber(e){
      let that=this
      wx.login({
        success: (res) => { 
          if(res.code){
            api.post('wechat/getUserPhone',{openIdCode:res.code,encryptedData:e.detail.encryptedData,code:e.detail.code,iv:e.detail.iv}).then(response=>{
                if(response.code===200){ 
                    let user=response.data;
                    debugger
                    wx.setStorageSync('user',JSON.stringify(user));  
                    if(user.isShow){
                        that.setData({
                            isAfterShow: true,
                            image: user.image
                           })
                    
                    }else{
                         that.backPage()
                        }
                    }else{
                        wx.showToast({
                            title:'登录失败',
                            icon: 'error',//图标，支持"success"、"loading" 
                            // image: '/images/load.gif',
                            duration: 1500,//提示的延迟时间，单位毫秒，默认：1500 
                            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false 
                          });
                      
                    }
                   
            }) 
        }
        },
      })
   
},
//用户协议
user_xy(){
  wx.navigateTo({
    url: '/pages/mine/contact_us/user_agreement/user_agreement',
  })
},
//用户隐私
user_ys(){
  wx.navigateTo({
    url: '/pages/mine/contact_us/privacy_policy/privacy_policy',
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
       
     this.setData({
         type: options.type,
         reportId: options.id,
         backType:options.backType
     })
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})